/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.opgea.qms.service.impl;

import com.opgea.qms.dao.CompanyDAO;
import com.opgea.qms.dao.LoginDAO;
import com.opgea.qms.domain.entities.Company;
import com.opgea.qms.domain.entities.Employee;
import com.opgea.qms.domain.entities.Login;
import com.opgea.qms.domain.model.CompanyStatus;
import com.opgea.qms.domain.model.CurrentTokenModel;
import com.opgea.qms.domain.model.MailModel;
import com.opgea.qms.domain.qualifiers.LoginQualifier;
import com.opgea.qms.listeners.TokenContextLoaderListener;
import com.opgea.qms.service.ApplicationService;
import com.opgea.qms.service.CompanyService;
import com.opgea.qms.service.EmployeeService;
import com.opgea.qms.service.mail.MailService;
import com.opgea.qms.web.form.CompanyDTO;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 *
 * @author Ramesh
 */
@Service
public class CompanyServiceImpl implements CompanyService{

    @Autowired
    private CompanyDAO companyDAO;
    
    @Autowired
    private LoginDAO loginDAO;
    
    @Autowired
    private MailService mailService;
    
    @Autowired
    private EmployeeService employeeDAO;
    
    @Autowired
    private ApplicationService applicationService;
    
    @Override
    public CompanyDTO create(CompanyDTO companyDTO) {
        Company company = new Company();
        company.setId(Calendar.getInstance().getTimeInMillis());
        company.setName(companyDTO.getName());

        Employee employee = new Employee();
        employee.setFirstName(companyDTO.getFirstName());
        employee.setMiddleInitial(companyDTO.getMiddleInitial());
        employee.setLastName(companyDTO.getLastName());
        employee.setEmailId(companyDTO.getEmail());
        employee.setPhoneNo(companyDTO.getContactNo());
        employee.setCompany(company);

        List<Employee> empList = new ArrayList<Employee>();
        empList.add(employee);
        
        company.setEmployeeList(empList);
        companyDAO.create(company);
        CompanyStatus companyStatus = new CompanyStatus();
        companyStatus.setCompanyId(company.getId());
        companyStatus.setName(company.getName());
        applicationService.addCompanyStatus(companyStatus);
        
        //Employee newEmployee = employeeDAO.find(employee.getId());
        Login login = new Login();
        login.setLoginId(companyDTO.getEmail());
        login.setPassword("default");
        login.setLoginQualifier(LoginQualifier.Master.ordinal());
        login.setEmployee(employee);
        loginDAO.create(login);

        /*
         * Mailing after confirming that company is registered
         * in OPGEA database.
         */
        
        String[] filePath = {
                              "C:\\attachments\\qms\\welcome\\opgea.jpg"
                            }; 
        //"C:\\attachments\\Guideline.docx"

        
        MailModel mailModel = new MailModel();
        mailModel.setTo(companyDTO.getEmail());
        mailModel.setFrom("shekharkumargupta@gmail.com");
        mailModel.setSubject("OPGEA Systems");
        mailModel.setBodyText("<html><b>Congratulations!</b> <br><br>" +
                             "Your Login Id has been created for <b>OPGEA QMS</b> Application as <br>" +
                             "<b>User Id : </b>"+login.getLoginId()+"<br> " +
                             "<b>Password : </b>"+login.getPassword()+"<br> " +
                             "Click on this link to Login: <a href='http://opgea.com/oqms/app/login'>Login</login>" +
                             "</html>");
        mailModel.setFilePath(filePath);

        mailService.sendMimeMail(mailModel);
        
        /*
         * Adding companies' Current Token Context to the TokenContextLoaderListener 
         */
        
        CurrentTokenModel currentToken = new CurrentTokenModel();
        currentToken.setCompanyId(company.getId());
        currentToken.setCurrentTokenNumber(0);
        currentToken.setToday(new Date());
        
        TokenContextLoaderListener.currentTokenMap.put(currentToken.getCompanyId(), currentToken);
        return companyDTO;
    }

    @Override
    public Company update(Company company) {
        return companyDAO.update(company);
    }

    @Override
    public Company delete(Long companyId) {
        return companyDAO.delete(companyId);
    }

    @Override
    public Company find(Long id) {
        return companyDAO.find(id);
        
    }

    @Override
    public List<Company> findAll() {
        return companyDAO.findAll();
    }
    
}
